package com.daykw.wkinfo.task;

import java.util.Iterator;

import org.apache.log4j.Logger;
import org.ehcache.Cache;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;

import com.alibaba.druid.pool.DruidDataSource;
import com.daykw.wkinfo.utils.EhcacheUtils;

public class ClearIpPoolTask implements Runnable {
	private static Logger logger = Logger.getLogger(ClearIpPoolTask.class);
	public static Dao taskDao;
	{
		DruidDataSource dataSource = new DruidDataSource();

		dataSource.setDriverClassName("com.mysql.jdbc.Driver");

		dataSource.setUrl("jdbc:mysql://211.152.47.69:3306/task_center?useUnicode=true&characterEncoding=UTF-8");

		dataSource.setUsername("new_root");
		dataSource.setPassword("@Hyipsos");
		dataSource.setTestOnBorrow(true);
		dataSource.setTestOnReturn(true);
		dataSource.setTestWhileIdle(true);
		dataSource.setValidationQuery("SELECT 1 ");
		taskDao = new NutDao(dataSource);
	}

	@Override
	public void run() {

		Iterator<Cache.Entry<String, String>> its = EhcacheUtils.getInstance().getCacheKeys();
		int count = 0;

		while (its.hasNext()) {
			Cache.Entry<String, String> kv = its.next();
			String key = kv.getKey();
			taskDao.clear("t_ips", Cnd.where("ips", "=", key));
			count++;
		}
		logger.info("#######清除了" + count + "个无用IP#######");
	}

}
